from google.appengine.ext import db
from site_model import site_model
from advert import advert_model

class Site():
    
    # Site name
    Name = ''
    # Feed URL
    FeedURL = ''
    # Host Header
    HostHeader = ''
    # IsValid
    IsValid = False
    # Dictionary of Adverts on this site
    Adverts = dict()
   
    
    # Load the site based on the host url
    def Load(self, hosturl):
        # Load the Site record from the DB
        
        dbsite = site_model.all()
        dbsite.filter('hostheader = ', hosturl)
        
        self.HostHeader = hosturl
        
        if dbsite.count > 0:
            self.Name = dbsite[0].name
            self.FeedURL = dbsite[0].feedurl
                                  
            self.LoadAdverts(dbsite[0])
            
            #t = Advert('TEST','New TEST Advert',dbsite[0] )
            #t.TestSetup()
            self.IsValid = True
        else:
            self.IsValid = False
            
    def LoadAdverts(self, parent):       
        for ad in parent.adverts:
            self.Adverts[ad.position] = ad.content
    
    
    
    def TestSetup(self):
        x = site_model()
        x.hostheader = 'default'
        x.name = 'News Mashup Feed'
        x.feedurl = 'http://pipes.yahoo.com/pipes/pipe.run?_id=5a0454d08207f2760c820cdba0e94b8b&_render=rss'
        x.put()
        
        ad = advert_model()
        ad.position = 'TEST POSITION'
        ad.content = 'TEST CODE'
        ad.site = x
        
        ad.put() 